import ShooterPlant from "../src/component/plant/ShooterPlant.js";

export class RectangleSprite extends Phaser.GameObjects.Sprite {
    constructor(scene, x, y, width, height, color) {
        super(scene, x, y, null); // 传入 null 作为纹理
        this.scene=scene
        this.width = width;
        this.height = height;
        this.color = color;

        // 添加到场景中
        scene.add.existing(this);

        // 创建用于绘制的图形对象
        //this.graphics = scene.add.graphics();
        this.graphics = scene.add.graphics();
        //this.drawRect();

        // 添加输入事件
        this.setInteractive(new Phaser.Geom.Rectangle(0, 0, width, height), Phaser.Geom.Rectangle.Contains);

        this.on('pointerover', () => this.setHover(true));
        this.on('pointerout', () => this.setHover(false));
        this.on('pointerdown', () => this.onClick());
        this.scene.events.on("update", this.update, this);
    }

    update() {
        this.graphics.clear(); // 确保这里调用时 graphics 存在
        this.graphics.fillStyle(this.color, 0);
       // this.graphics.fillRect(this.x , this.y, this.width, this.height);
        this.graphics.fillRect(this.x - 16, this.y -16, this.width, this.height);
    }

    setHover(isHovered) {
        this.color = isHovered ? 0xff0000 : 0x00ff00; // 悬浮时变为红色，非悬浮时为绿色
        //this.drawRect(); // 重新绘制
    }

    onClick() {
        console.log(`长方形被点击了！${this.scene.curPlant}`);
        new ShooterPlant(this.scene, this.x+this.width/2, this.y+this.height/2,1);
        // 在这里处理点击事件
    }

    // 更新的位置和大小
    setPosition(x, y) {
        super.setPosition(x, y);
      //  this.drawRect();
    }
}